import pandas as pd
import json
def to_json():
    
    file = pd.read_csv('./dataset/train_prompt.csv')

    coloums = file[['word_mf2','label_raw','c_numerical','prompt']]

    json_string = coloums.to_json(orient="records")
    json_data = json.loads(json_string)
    file_path = './dataset/train_prompt.json'
    # 使用open()函数创建文件并将JSON数据写入文件
    with open(file_path, 'w') as json_file:
        for data in json_data:
            json.dump(data, json_file, ensure_ascii=False)  # 使用indent参数设置缩进，可选

    print(f'JSON数据已保存到文件：{file_path}')
    
def to_json_with_prompt(label, file_path):
    df = pd.read_csv(file_path)
    coloums = df[['word_mf2','label_raw','c_numerical']]
    prompt = []
    
    for index, row in coloums.iterrows():
        ques = row['word_mf2']
        p = "这是一个对话意图识别任务。在给定意图标签列表的条件下，每个对话只有一个意图，你需要回答用户的唯一对话意图。 \
                意图列表: " + label \
                + '\n 输入对话: ' + ques +' \n对话意图预测结果为: '    
                        # + ques + "\n#输出标签:"+ label + "请为输入选择对应的标签"
        prompt.append(p)
    coloums['prompt'] = prompt
    coloums.to_csv('test_prompt_siming.csv')
    
# end to_json
def count_label(file_path):
    df = pd.read_csv(file_path)
    label_array = []
    for index, row in df.iterrows():
        label = row['label_raw']
        if label not in label_array:
            label_array.append(label)
    # 打开一个文本文件用于写入（'w' 模式表示写入）
    with open('label_train.txt', 'w') as file:
        # 将列表的每个元素写入文件中
        for item in label_array:
            file.write('\n#'+item + '#')
    
def read_label(txt_file):
    label = ''
    with open(txt_file, 'r', encoding='utf-8') as file:
        for line in file:
            label += line
    # print(label)
    return label


if __name__ == "__main__":
    path = './dataset/test.csv'
    # count_label(path)
    txt = './dataset/label_test.txt'
    label = read_label(txt)
    to_json_with_prompt(label, path)
    # to_json()
    